package controllers

import (
	"classmates/models"
	"encoding/json"

	beego "github.com/beego/beego/v2/server/web"
)

// ClassmatesController operations for Classmates
type ClassmatesController struct {
	beego.Controller
}

func (c *ClassmatesController) Login() {
	c.TplName = "login.html"
}
func (c *ClassmatesController) Home() {
	c.TplName = "home.html"
}

func (c *ClassmatesController) Records() {
	c.TplName = "records.html"
}
func (c *ClassmatesController) Info() {
	c.TplName = "info.html"
}

// Post ...
// @Title 登录
// @Description 登录
// @Param   sn      query   string  true        "用户名"
// @Param   pswd    query   string  true        "密码"
// @Success 201 {int} models.Classmates
// @Failure 403 body is empty
// @router /login [post]
func (c *ClassmatesController) DoLogin() {
	var v models.Classmates
	result := make(map[string]interface{})
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err != nil {
		result["code"] = 0
		result["data"] = err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	target := models.AllClassMates[v.Sn]
	if target == nil {
		result["code"] = 0
		result["data"] = "查无此人"
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	if v.PSWD != target.PSWD {
		result["code"] = 0
		result["data"] = "密码不对"
		c.Data["json"] = result
		c.ServeJSON()
		return
	}

	result["code"] = 1
	result["data"] = target
	result["courseList"] = models.AllCourse
	result["jwt"], _ = GetJwtString(12, target.Id, target.Sn)
	c.Data["json"] = result
	c.ServeJSON()
}

// Put ...
// @Title 修改密码
// @Description 修改密码
// @Param	pswdvalue		path 	string	true		"新密码"
// @Success 200 {object} models.Classmates
// @Failure 403 :id is not int
// @router /pswd/:pswdvalue [put]
func (c *ClassmatesController) UpdatePswd() {
	idObj := c.Ctx.Input.GetData("id")
	idfloat := idObj.(float64)
	snObj := c.Ctx.Input.GetData("sn")
	snStr := snObj.(string)
	pswd := c.Ctx.Input.Param(":pswdvalue")
	newitem, err := models.UpdatePswdById(int64(idfloat), snStr, pswd)
	result := make(map[string]interface{})
	if err != nil {
		result["code"] = 0
		result["data"] = err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	models.AllClassMates[snStr].PSWD = pswd

	result["code"] = 1
	result["data"] = newitem
	c.Data["json"] = result
	c.ServeJSON()

}

// Put ...
// @Title 修改资料
// @Description 修改资料
// @Param	body		body 	models.Classmates	true		"body for Classmates content"
// @Success 200 {object} models.Classmates
// @Failure 403 :id is not int
// @router /updateInfo/mine [put]
func (c *ClassmatesController) UpdateInfo() {
	var v models.Classmates
	result := make(map[string]interface{})
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err != nil {
		result["code"] = 0
		result["data"] = err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	snObj := c.Ctx.Input.GetData("sn")
	snStr := snObj.(string)
	if models.AllClassMates[snStr] == nil {
		result["code"] = 0
		result["data"] = "查无此人,sn:" + snStr
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	models.AllClassMates[snStr].Email = v.Email
	models.AllClassMates[snStr].Tel = v.Tel
	models.AllClassMates[snStr].Comments = v.Comments
	err := models.UpdateClassmatesById(models.AllClassMates[snStr])
	if err != nil {
		result["code"] = 0
		result["data"] = err.Error()
		c.Data["json"] = result
		c.ServeJSON()
		return
	}
	result["code"] = 1
	result["data"] = models.AllClassMates[snStr]
	c.Data["json"] = result
	c.ServeJSON()

}
